package com.kk.permission;

import android.accessibilityservice.AccessibilityService;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;

import com.kk.permission.events.ServiceInterruptEvent;
import com.kk.permission.utils.callershow.CallerShowUtils;
import com.kk.permission.utils.callershow.RxBus;

public final class PermissionAccessibilityService extends AccessibilityService {
    private static final String TAG = "PAccessibilityService";
    private static Listener sListener;

    public interface Listener {
        void onEvent(AccessibilityEvent accessibilityEvent);
    }

    public static void setListener(Listener listener) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("setListener() ");
        stringBuilder.append(listener);
        sListener = listener;
    }

    public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("onAccessibilityEvent() type=");
        stringBuilder.append(accessibilityEvent.getEventType());
        if (sListener != null) {
            sListener.onEvent(accessibilityEvent);
        }
        Log.d(TAG, accessibilityEvent.getEventType()+"");
    }

    public void onInterrupt() {
        RxBus.getIns().post(new ServiceInterruptEvent());
    }

    protected void onServiceConnected() {
        super.onServiceConnected();
        RxBus.getIns().post(this);
        CallerShowUtils.tagOpenAccessibitySetting(this, true);
    }
}
